package gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import game.Game;

import javax.swing.*;

public class NewGamePanel extends JPanel implements ActionListener {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Game game;
	
	private JFrame frame;
	
	private JButton okButton;
	private JButton cancelButton;
	
	private ButtonGroup player1ButtonGroup;
	private ButtonGroup player2ButtonGroup;
	
	public NewGamePanel(Game game, JFrame frame)
	{
		this.game = game;
		this.frame = frame;
		
		JPanel player1Panel = new JPanel(new GridLayout(0, 1));

		JRadioButton player1Human = new JRadioButton("Human");
		player1Human.setActionCommand(""+Game.AI_HUMAN);
		player1Human.setSelected(true);
		
		JRadioButton player1Random = new JRadioButton("Random");
		player1Random.setActionCommand(""+Game.AI_RANDOM);
		
		JRadioButton player1Minmax = new JRadioButton("Minmax");
		player1Minmax.setActionCommand(""+Game.AI_MINMAX);
		
		JRadioButton player1AlphaBeta = new JRadioButton("AlphaBeta");
		player1AlphaBeta.setActionCommand(""+Game.AI_ALPHABETA);
		
		JRadioButton player1MinmaxImproved = new JRadioButton("MinmaxImproved");
		player1MinmaxImproved.setActionCommand(""+Game.AI_MINMAXIMPROVED);
		
		JRadioButton player1AlphaBetaImproved = new JRadioButton("AlphaBetaImproved");
		player1AlphaBetaImproved.setActionCommand(""+Game.AI_ALPHABETAIMPROVED);
		
		JRadioButton player1MinMax2 = new JRadioButton("MinMax2");
		player1MinMax2.setActionCommand(""+Game.AI_MINMAX2);

		//JRadioButton player1AlphaBetaTable = new JRadioButton("AlphaBetaTable");
		//player1AlphaBetaTable.setActionCommand(""+Game.AI_ALPHABETATABLE);

		JRadioButton player1AlphaBetaMultiThread = new JRadioButton("AlphaBetaMultiThread");
		player1AlphaBetaMultiThread.setActionCommand(""+Game.AI_ALPHABETAMULTITHREADED);
		
		this.player1ButtonGroup = new ButtonGroup();
		this.player1ButtonGroup.add(player1Human);
		this.player1ButtonGroup.add(player1Random);
		this.player1ButtonGroup.add(player1Minmax);
		this.player1ButtonGroup.add(player1AlphaBeta);
		this.player1ButtonGroup.add(player1MinmaxImproved);
		this.player1ButtonGroup.add(player1AlphaBetaImproved);
		this.player1ButtonGroup.add(player1MinMax2);
		//this.player1ButtonGroup.add(player1AlphaBetaTable);
		this.player1ButtonGroup.add(player1AlphaBetaMultiThread);
		
		player1Panel.add(player1Human);
		player1Panel.add(player1Random);
		player1Panel.add(player1Minmax);
		player1Panel.add(player1AlphaBeta);
		player1Panel.add(player1MinmaxImproved);
		player1Panel.add(player1AlphaBetaImproved);
		player1Panel.add(player1MinMax2);
		//player1Panel.add(player1AlphaBetaTable);
		player1Panel.add(player1AlphaBetaMultiThread);
				
		
		

		JPanel player2Panel = new JPanel(new GridLayout(0, 1));
		
		JRadioButton player2Human = new JRadioButton("Human");
		player2Human.setActionCommand(""+Game.AI_HUMAN);
		player2Human.setSelected(true);
		
		JRadioButton player2Random = new JRadioButton("Random");
		player2Random.setActionCommand(""+Game.AI_RANDOM);
		
		JRadioButton player2Minmax = new JRadioButton("Minmax");
		player2Minmax.setActionCommand(""+Game.AI_MINMAX);
		
		JRadioButton player2AlphaBeta = new JRadioButton("AlphaBeta");
		player2AlphaBeta.setActionCommand(""+Game.AI_ALPHABETA);
		
		JRadioButton player2MinmaxImproved = new JRadioButton("MinmaxImproved");
		player2MinmaxImproved.setActionCommand(""+Game.AI_MINMAXIMPROVED);
		
		JRadioButton player2AlphaBetaImproved = new JRadioButton("AlphaBetaImproved");
		player2AlphaBetaImproved.setActionCommand(""+Game.AI_ALPHABETAIMPROVED);
		
		JRadioButton player2MinMax2 = new JRadioButton("MinMax2");
		player2MinMax2.setActionCommand(""+Game.AI_MINMAX2);

		//JRadioButton player2AlphaBetaTable = new JRadioButton("AlphaBetaTable");
		//player2AlphaBetaTable.setActionCommand(""+Game.AI_ALPHABETATABLE);

		JRadioButton player2AlphaBetaMultiThread = new JRadioButton("AlphaBetaMultiThread");
		player2AlphaBetaMultiThread.setActionCommand(""+Game.AI_ALPHABETAMULTITHREADED);
		
		this.player2ButtonGroup = new ButtonGroup();
		this.player2ButtonGroup.add(player2Human);
		this.player2ButtonGroup.add(player2Random);
		this.player2ButtonGroup.add(player2Minmax);
		this.player2ButtonGroup.add(player2AlphaBeta);
		this.player2ButtonGroup.add(player2MinmaxImproved);
		this.player2ButtonGroup.add(player2AlphaBetaImproved);
		this.player2ButtonGroup.add(player2MinMax2);
		//this.player2ButtonGroup.add(player2AlphaBetaTable);
		this.player2ButtonGroup.add(player2AlphaBetaMultiThread);

		player2Panel.add(player2Human);
		player2Panel.add(player2Random);
		player2Panel.add(player2Minmax);
		player2Panel.add(player2AlphaBeta);
		player2Panel.add(player2MinmaxImproved);
		player2Panel.add(player2AlphaBetaImproved);
		player2Panel.add(player2MinMax2);
		//player2Panel.add(player2AlphaBetaTable);
		player2Panel.add(player2AlphaBetaMultiThread);
		
		
		
		//The label panel
		JPanel labelPanel = new JPanel(new GridLayout(1, 2));
		labelPanel.add(new JLabel("Player 1 (blue)"));
		labelPanel.add(new JLabel("Player 2 (red)"));
		
		
		//The radiobutton panel
		JPanel radioPanel = new JPanel(new GridLayout(1, 2));
		radioPanel.add(player1Panel);
		radioPanel.add(player2Panel);
		
		
		//The button panel
		this.okButton = new JButton("Ok");
		this.cancelButton = new JButton("Cancel");
		this.okButton.addActionListener(this);
		this.cancelButton.addActionListener(this);
		
		JPanel buttonPanel = new JPanel(new GridLayout(1, 2));
		buttonPanel.add(this.okButton);
		buttonPanel.add(this.cancelButton);
		
		
		//Add the panels to the final layout
		this.setLayout(new BorderLayout());
		this.add(labelPanel, BorderLayout.NORTH);
		this.add(radioPanel, BorderLayout.CENTER);
		this.add(buttonPanel, BorderLayout.SOUTH);
	}

	public void actionPerformed(ActionEvent actionEvent)
	{
		if (actionEvent.getSource() == this.okButton)
		{
			try
			{
				this.game.start(
					Integer.parseInt(this.player1ButtonGroup.getSelection().getActionCommand()),
					Integer.parseInt(this.player2ButtonGroup.getSelection().getActionCommand()));
			}
			catch (NumberFormatException e)
			{
				System.out.println("NewGamePanel: NumberFormatException");
				return;
			}
			finally
			{
				this.frame.setVisible(false);
			}
		}
		else if (actionEvent.getSource() == this.cancelButton)
		{
			this.frame.setVisible(false);
		}
		
	}
	
}
